﻿namespace JumpJump.Runtime
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    [Serializable]
    public class Replay
    {
        #region Private Fields

        /// <summary>
        /// the path stored replays
        /// </summary>
        private string _replayPath = System.IO.Directory.GetCurrentDirectory() + "\\replays\\";

        #endregion

        #region Public Propertites

        /// <summary>
        /// When the Game started
        /// </summary>
        public DateTime GameDate { get; set; }

        /// <summary>
        /// AI and User movement
        /// </summary>
        public List<Movement> PlayersMovementList { get; set; }

        /// <summary>
        /// Users piece coordinate after last move
        /// </summary>
        public List<Coordinate> UserPiecesCoordinate { get; set; }

        /// <summary>
        /// Prick piece coordinate after last move
        /// </summary>
        public List<Coordinate> PrickPiecesCoordinate { get; set; }

        /// <summary>
        /// if this is true, show the board when it save
        /// </summary>
        public bool IsShowLastBoard = false;
        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the Replay class.
        /// </summary>
        /// <param name="date">start date</param>
        public Replay(DateTime date)
        {
            this.GameDate = date;
            Initial();
        }

        /// <summary>
        /// Initializes a new instance of the Replay class.
        /// </summary>
        /// <param name="date">start date</param>
        /// <param name="move">movement list</param>
        public Replay(DateTime date, List<Movement> move)
        {
            this.GameDate = date;
            this.PlayersMovementList = move;
            Initial();
        }

        #endregion

        #region Private Methods



        #endregion

        #region  Public Methods

        /// <summary>
        /// load replays from file
        /// </summary>
        /// <param name="fileName">file name</param>
        public static Replay Load(string fileName)
        {
            Replay rep;
            FileStream fs = new FileStream(fileName, FileMode.Open);
            BinaryFormatter bf = new BinaryFormatter();
            rep = bf.Deserialize(fs) as Replay;
            fs.Close();
            return rep;
        }

        /// <summary>
        /// save replay to file
        /// </summary>
        public void Save()
        {
            string fileName = GameDate.ToString();
            fileName = fileName.Replace(" ", "");
            fileName = fileName.Replace(":", "");
            if (!Directory.Exists(_replayPath))
            {
                Directory.CreateDirectory(_replayPath);
            }
            fileName = _replayPath + fileName + ".rep";
            FileStream fs = new FileStream(fileName, FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs, this);
            fs.Close();
        }

        /// <summary>
        /// initial 
        /// </summary>
        public void Initial()
        {
            this.PlayersMovementList = new List<Movement>();
        }

        /// <summary>
        /// insert the last move
        /// </summary>
        /// <param name="move">last move</param>
        public void InsertLastMove(Movement move)
        {
            this.PlayersMovementList.Add(move);
        }

        /// <summary>
        /// save all the pieces position
        /// </summary>
        /// <param name="userPositions"></param>
        /// <param name="prickPositions"></param>
        public void SaveLastPiecesPosition(List<Coordinate> userPositions, List<Coordinate> prickPositions)
        {
            this.UserPiecesCoordinate = userPositions;
            this.PrickPiecesCoordinate = prickPositions;
        }

        #endregion
    }
}
